package com.pri.chat.fragment.daohang;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nim.uikit.business.recent.GlideImageLoader;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BannerBean;
import com.netease.nim.uikit.net.entity.BannerModel;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.JJBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.MoneyNoteListActivity;
import com.pri.chat.activity.PlayGameActivity;
import com.pri.chat.activity.ShareMemberActivity;
import com.pri.chat.activity.WalletActivity;
import com.pri.chat.base.TcWebActivity;
import com.pri.chat.dialog.DialogUtil;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.fragment.FragmentLazy;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/11
 */
public class WishMsgFragment extends FragmentLazy {


    @BindView(R.id.rx_progress)
    TextView rxProgress;
    @BindView(R.id.tv_jj_dh)
    TextView tvJjDh;
    @BindView(R.id.tv_wish_price)
    TextView tvWishPrice;
    @BindView(R.id.tv_jj_tx)
    TextView tvJjTx;


    @BindView(R.id.yyjjsmImg)
    ImageView yyjjsmImg;
    @BindView(R.id.tv_friends_speed)
    TextView tvFriendsSpeed;
    @BindView(R.id.iv_wish_top)
    Banner ivWishTop;
    @BindView(R.id.tv_play_game)
    TextView tvPlayGame;
    @BindView(R.id.tv_jj_xq)
    TextView tvJjXq;
    Unbinder unbinder;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

        }
    };
    BigDecimal mMoney = new BigDecimal("0.000000");
    BigDecimal mMin = new BigDecimal("0.000001");
    BigDecimal mDay = new BigDecimal("0.1");
    private int type;
    private int MAX_TIMER = 86400;
    private CountDownTimer timer;
    private boolean isFinish = false;
    private int count_time;
    private List<String> img_list = new ArrayList<>();
    private List<BannerBean> lunbolistBeanList = new ArrayList<>();

    public static WishMsgFragment getInstance(int type) {
        WishMsgFragment fragment = new WishMsgFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_wish_msg_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initLayout();
        initBanner();
        return view;
    }

    private void initLayout() {
    }

    private void initBanner() {
        ivWishTop.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                String path = lunbolistBeanList.get(position).getLinkPath();
                Intent intent = new Intent();
                intent.setClass(getActivity(), TcWebActivity.class);
                intent.putExtra("url", path);
                startActivity(intent);
            }
        }).setImageLoader(new GlideImageLoader());
    }

    @Override
    protected void initData() {
        httpGetBanner();
        httpGetJJ();
    }

    public void getWishMoney(){
        httpGetJJ();
    }

    private void httpGetBanner() {
        SubscriberOnNextListener<BaseBean<List<BannerBean>>> onNextListener = baseBean -> {
            if (img_list.isEmpty()) {
                lunbolistBeanList.addAll(baseBean.getData());
                for (int i = 0; i < lunbolistBeanList.size(); i++) {
                    img_list.add(lunbolistBeanList.get(i).getBannerPath());
                }
                ivWishTop.setImages(img_list);
                ivWishTop.start();
            }
        };
        BannerModel evaListModel = new BannerModel("yyjjBanner");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().getBannerList(new ProgressSubscriber<>(onNextListener, getContext(), false), requestBody);
    }

    BigDecimal timer_2;
    private void httpGetJJ() {
        SubscriberOnNextListener<BaseBean<JJBean>> onNextListener = baseBean -> {
            tvWishPrice.setText(baseBean.getData().getYyjjMoney());
            if(Double.parseDouble(baseBean.getData().getYyjjMoney())>=50d){
                rxProgress.setText("0.0");
            }else{
                BigDecimal timer_1;
                if(Double.parseDouble(baseBean.getData().getYyjjMoney())<49d){
                    timer_1 = new BigDecimal(baseBean.getData().getTimer()).divide(new BigDecimal(1000000));
                    timer_2 = new BigDecimal(baseBean.getData().getBegin()).divide(new BigDecimal(10));
                }else{

                    mMoney = new BigDecimal("0.0000000");
                    mMin = new BigDecimal("0.0000001");
                    mDay = new BigDecimal("0.01");

                    timer_1 = new BigDecimal(baseBean.getData().getTimer()).divide(new BigDecimal(10000000));
                    timer_2 = new BigDecimal(baseBean.getData().getBegin()).divide(new BigDecimal(100));
                }
                mMoney = timer_1;
                count_time = MAX_TIMER - baseBean.getData().getTimer();
                if (!baseBean.getData().getBegin().equals("2")) {
                    rxProgress.setText(mMoney.add(timer_2).toPlainString());
                    countDown();
                    timer.start();
                } else {
                    rxProgress.setText(timer_2.toPlainString());
                }
            }
        };
        UserModel userModel = new UserModel();
        userModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(userModel));
        HttpMethods.getInstance().getYwjjInfo(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    private void countDown() {
        timer = new CountDownTimer(count_time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mMoney = mMoney.add(mMin);
                rxProgress.setText(mMoney.add(timer_2).toPlainString());
            }

            @Override
            public void onFinish() {
                if (!isFinish) {
                    mMoney = mDay;
                    rxProgress.setText(mMoney.add(timer_2).toPlainString());
                    count_time = MAX_TIMER;
                    timer.start();
                }
            }
        };


    }

    private void initSrl() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == 2) {
            if (timer != null) {
                timer.cancel();
            }
            httpGetJJ();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFinish = true;
        if(timer != null){
            timer.cancel();
        }
    }

    @OnClick({R.id.yyjjsmImg,R.id.tv_jj_dh, R.id.tv_jj_xq, R.id.tv_jj_tx, R.id.tv_friends_speed, R.id.iv_wish_top, R.id.tv_play_game})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_jj_dh:
                dh();
                break;
            case R.id.tv_jj_xq:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                RxActivityTool.skipActivity(mContext, MoneyNoteListActivity.class, bundle);
                break;
            case R.id.tv_jj_tx:
                RxActivityTool.skipActivityForResult(mContext, WalletActivity.class, 2);
                break;
            case R.id.tv_friends_speed:
                initShareDialog("");
                break;
            case R.id.iv_wish_top:
                break;
            case R.id.tv_play_game:
                RxActivityTool.skipActivity(mContext, PlayGameActivity.class);
                break;
            case R.id.yyjjsmImg:
                Intent intent = new Intent();
                intent.setClass(getContext(), TcWebActivity.class);
                intent.putExtra("title","愿望金说明");
                intent.putExtra("url", HttpMethods.BASE_URL+"p/front_editorHtmlShow.html?code=yuanwangjijinCode");
                startActivity(intent);
                break;
        }
    }

    private Dialog mShareDialog;

    private void initShareDialog(String path) {
        mShareDialog = DialogUtil.getInstance().showShareDialog(mContext, 1);
        DialogUtil.getInstance().setOnShareClickListener(new DialogUtil.OnShareClickListener() {
            @Override
            public void qqOnClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("id",SharedHelper.readId(getContext()));
                bundle.putString("image",SharedHelper.readHeadPic(getContext()));
                bundle.putString("title","给我加速吧");
                bundle.putInt("type",3);
                bundle.putString("content","帮我加速愿望金吧！");
                RxActivityTool.skipActivity(mContext, ShareMemberActivity.class,bundle);
                mShareDialog.dismiss();mShareDialog.dismiss();
            }

            @Override
            public void circleOnClick(View v) {
                mShareDialog.dismiss();
                DialogUtil.getInstance().shareWechatMoments(SharedHelper.readHeadPic(getContext()),HttpMethods.SHARE_TITLE,"这里有愿望金哦，快来帮我加速吧！",HttpMethods.SHARE_URL);
            }

            @Override
            public void qZoneOnClick(View v) {

            }

            @Override
            public void weixinOnClick(View v) {
                mShareDialog.dismiss();
                DialogUtil.getInstance().shareWeChat(SharedHelper.readHeadPic(getContext()),HttpMethods.SHARE_TITLE,"这里有愿望金哦，快来帮我加速吧！",HttpMethods.SHARE_URL);
            }
            @Override
            public void jubaoOnClick(View v) {
            }
        });
        mShareDialog.show();
    }

    private void dh() {
        SubscriberOnNextListener<BaseBean<JJBean>> onNextListener = baseBean -> {
            if (timer != null) {
                timer.cancel();
            }
            httpGetJJ();
        };
        UserModel userModel = new UserModel();
        userModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(userModel));
        HttpMethods.getInstance().duiHuanYyjj(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);

    }
}
